<?php

namespace app\beautiful\controller\admin\appointment;

use app\BaseController;

use app\beautiful\model\appointment\PerformanceItemModel;

use app\beautiful\model\appointment\PerformanceModel;

use app\beautiful\model\StaffModel;

use app\common\logic\DataLogic;

use think\facade\Db;





class Performance extends BaseController{

    protected $name="业绩";



    public function index()

    {

        $staff= $this->request->param('staff');

        $member=$this->request->param('member');

        $status=$this->request->param('status/d',1);

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['status','=',$status],

        ];

        

        if(!empty($staff)){

            $staff_ids=StaffModel::where([

                ['shop_id','=',$this->request->shopId],

                ['is_delete','=',0],

                ['status','=',1],

                ['name|mobile','=',$staff]

            ])->column('staff_id') ?? [];

            $where[]=['staff_id','in',$staff_ids];

        }

        

        if(!empty($member)) $where[]=['mobile','=',$member];

        

        $performance_model=new PerformanceModel();

        $performance_model = $performance_model->with(['items','staff']);

        $order=['add_time'=>'desc'];

        $field='*';

        $append=['add_time_format','status_means'];

        $return = DataLogic::getDataList($performance_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return);

    }



    public function approval()

    {

        $performance_id = $this->request->param('performance_id/d',0);

        $status=$this->request->param('status/d',0);



        if($performance_id==0) return $this->error($this->name.'不存在');

        if(!in_array($status,[-1,8])) return $this->error('参数错误');



        if(null === $performance = PerformanceModel::find($performance_id)) return $this->error($this->name.'不存在');

        if($performance->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');

        if($performance->status != 1) return $this->error($this->name.'不在审核状态');



        Db::startTrans();

        try{

            $performance->status=$status;

            $performance->save();



            $performance_item_data=PerformanceItemModel::where([

                ['shop_id','=',$this->request->shopId],

                ['performance_id','=',$performance_id]

            ])->field('performance_item_id,status')->select()->toArray();

            if(!empty($performance_item_data)){

                foreach ($performance_item_data as $k => $v){

                    $performance_item_data[$k]['status']=$status;

                }

                $performance_item_model= new PerformanceItemModel();

                $performance_item_model->saveAll($performance_item_data);

            }

            Db::commit();

            return $this->success('操作成功');

        }catch (\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }



    }



}

